﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TKEY.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records
{
    #region Usings

    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Globalization;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The TKEY record.
    /// </summary>
    public sealed class TKEY : Record
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TKEY"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public TKEY(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            this.Algorithm = recordReader.ReadDomainName();
            this.Inception = recordReader.ReadUInt32();
            this.Expiration = recordReader.ReadUInt32();
            this.Mode = recordReader.ReadUInt16();
            this.Error = recordReader.ReadUInt16();
            this.KeySize = recordReader.ReadUInt16();
            this.KeyData = recordReader.ReadBytes(this.KeySize);
            this.OtherSize = recordReader.ReadUInt16();
            this.OtherData = recordReader.ReadBytes(this.OtherSize);
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="TKEY" /> class.
        /// </summary>
        public TKEY()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Algorithm.
        /// </summary>
        public string Algorithm { get; private set; }

        /// <summary>
        ///     Gets Error.
        /// </summary>
        public ushort Error { get; private set; }

        /// <summary>
        ///     Gets Expiration.
        /// </summary>
        public uint Expiration { get; private set; }

        /// <summary>
        ///     Gets Inception.
        /// </summary>
        public uint Inception { get; private set; }

        /// <summary>
        ///     Gets Key data.
        /// </summary>
        public IEnumerable<byte> KeyData { get; private set; }

        /// <summary>
        ///     Gets Key size.
        /// </summary>
        public ushort KeySize { get; private set; }

        /// <summary>
        ///     Gets Mode.
        /// </summary>
        public ushort Mode { get; private set; }

        /// <summary>
        ///     Gets Other data.
        /// </summary>
        public IEnumerable<byte> OtherData { get; private set; }

        /// <summary>
        ///     Gets Other size.
        /// </summary>
        public ushort OtherSize { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format(
                CultureInfo.InvariantCulture, 
                "{0} {1} {2} {3} {4}", 
                this.Algorithm, 
                this.Inception, 
                this.Expiration, 
                this.Mode, 
                this.Error);
        }

        #endregion
    }
}